package linker;

import java.io.IOException;
import java.net.ServerSocket;

import linker.exception.ErrorClosingSocketException;
import linker.exception.ErrorCreatingClientSocketException;
import linker.exception.ErrorCreatingServerSocketException;

public class ServerLinker {
	
	private ServerSocket	serverSocket;
	
	public ServerLinker(int port) throws ErrorCreatingServerSocketException {

		try {
			this.serverSocket = new ServerSocket(port);
			System.out.println("Server Socket created.");
			System.out.println("Server runing..." + !(this.serverSocket == null));
			
		} catch (IOException e) {
			e.printStackTrace();
			throw new ErrorCreatingServerSocketException(e);
		}
	}
	
	public ClientLinker acceptNewClient() throws ErrorCreatingClientSocketException {

		try {
			return new ClientLinker(this.serverSocket.accept());
		} catch (IOException e) {
			e.printStackTrace();
			throw new ErrorCreatingClientSocketException(e);
		}
	}
	
	public void close() throws ErrorClosingSocketException {

		try {
			this.serverSocket.close();
		} catch (IOException e) {
			e.printStackTrace();
			throw new ErrorClosingSocketException(e);
		}
	}
	
}
